package com.app.micaihu.custom.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.app.micaihu.R;
import com.app.micaihu.app.AppApplication;
import com.app.utils.util.AppUtils;
import com.app.utils.util.ViewSizeUtils;

/**
 * Created by Guo on 2016/7/11.
 *
 * 自定义的军衔图标
 *
 *
 */
public class RankLevelView extends RelativeLayout {

    private Paint linePaint;
    private Paint pointPaint;
    private int lineColor;
    private float rootLeft;
    private float rootTop;
    private float rootRight;
    private float rootBottom;

    private float rootMiddle;
    private float radius;


    private float persent = 1;
    private float alreadyEnd;


    //左右两个军衔的drawable

    private String rankTextLeft = "";
    private String rankTextRight = "";
    private Paint rankPaint;
    private Paint pointPaint2;
    private Paint linePaint2;

    private float firstCircleStart;
    private float firstCircleEnd;
    private float secondCircleStart;
    private float secondCircleEnd;
    private Paint paintStroke;
    private float rootCirclePosition;
    private float rootTextViewPosition;


    public RankLevelView(Context context) {
        this(context,null);
    }

    public RankLevelView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public RankLevelView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initView(context);

    }

    private Context mContext;
    //是否绘制
    private boolean drawLine = true;

    private static int widthPixels;//屏幕的宽
    private static int heightPixels;//屏幕的高



    private void initView(Context context) {
        this.mContext = context;

        //部分ViewGroup会省略onDraw,添加上这一行 可以强制调用onDraw
        setWillNotDraw(false);
        getScreenSize();

        int dp = ViewSizeUtils.dp2px(AppApplication.getInstance(), 5);
        int dp2 = ViewSizeUtils.dp2px(AppApplication.getInstance(), 1);

        //白色画笔
        linePaint = new Paint();
        linePaint.setAntiAlias(true);
        linePaint.setDither(true);
        linePaint.setColor(Color.WHITE);
        linePaint.setStrokeWidth(dp);
        linePaint.setStyle(Paint.Style.FILL_AND_STROKE);

        //灰色线条
        linePaint2 = new Paint();
        linePaint2.setStrokeWidth(dp);
        linePaint2.setAntiAlias(true);
        linePaint2.setDither(true);
        linePaint2.setStyle(Paint.Style.FILL_AND_STROKE);
        linePaint2.setColor(mContext.getResources().getColor(R.color.trans_white_0));

        //第一个圆
        pointPaint = new Paint();
        pointPaint.setAntiAlias(true);
        pointPaint.setDither(true);
        pointPaint.setColor(Color.WHITE);
        pointPaint.setStyle(Paint.Style.FILL_AND_STROKE);

        //第二个圆
        pointPaint2 = new Paint();
        pointPaint2.setAntiAlias(true);
        pointPaint2.setDither(true);
        pointPaint2.setStyle(Paint.Style.FILL);
        pointPaint2.setColor(mContext.getResources().getColor(R.color.trans_white_8));


        //描边
        paintStroke = new Paint();
        paintStroke.setAntiAlias(true);
        paintStroke.setDither(true);
        paintStroke.setStyle(Paint.Style.STROKE);
        paintStroke.setStrokeWidth(dp2);
        paintStroke.setColor(mContext.getResources().getColor(R.color.common_bg_color_1));



        rankPaint = new Paint();
        rankPaint.setAntiAlias(true);
        rankPaint.setDither(true);
        rankPaint.setColor(mContext.getResources().getColor(R.color.common_font_color_11));
        rankPaint.setTextAlign(Paint.Align.CENTER);
        rankPaint.setStyle(Paint.Style.STROKE);

        rankPaint.setTextSize(mContext.getResources().getDimension(R.dimen.common_font_size13));

    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        calculateLoaction();
        if(drawLine){
            drawView(canvas);
        }

    }




    private void calculateLoaction() {
        rootLeft = getLeft();//线开始的位置
        rootTop = getTop();
        rootRight = getRight();//结束的位置
        rootBottom = getBottom();
        radius = widthPixels/24;//圆形图标的半径
        float circle = widthPixels/12;//圆的直径
        //LinearLayout的中间点
        rootMiddle = (rootBottom - rootTop)/2 ;


        rootTextViewPosition = rootMiddle + radius*2 + 3;

        //第一个圆的位置
        firstCircleStart = radius * 2;
        firstCircleEnd = radius * 4;

        //第二个圆的位置
        secondCircleStart = radius * 20;
        secondCircleEnd = radius * 22;

        //中间线的长度
        float middleLine = secondCircleStart - firstCircleEnd;

        float already = (middleLine * persent);

        alreadyEnd = already + firstCircleEnd;

    }

    /**
     * 绘制视图
     *
     * @param canvas
     */
    private void drawView(Canvas canvas) {

        //画第一个圆
        canvas.drawCircle(firstCircleStart + radius,rootMiddle,radius,pointPaint);
        //画中间的线-已完成的
        canvas.drawLine(firstCircleEnd,rootMiddle,alreadyEnd,rootMiddle,linePaint);
        //画中间的线-未完成的
        canvas.drawLine(alreadyEnd,rootMiddle,secondCircleStart,rootMiddle,linePaint2);



        //画第二个圆
        canvas.drawCircle(secondCircleStart+radius,rootMiddle,radius,pointPaint2);


        //画三个圆的描边
        canvas.drawCircle(firstCircleStart + radius,rootMiddle,radius+1,paintStroke);
        canvas.drawCircle(secondCircleStart+radius,rootMiddle,radius+1,paintStroke);



        //中间进度的圆
        canvas.drawCircle(alreadyEnd,rootMiddle,radius/4,pointPaint);
        canvas.drawCircle(alreadyEnd,rootMiddle,radius/4,paintStroke);

        //画左边的字
        if(!TextUtils.isEmpty(rankTextLeft)){
            canvas.drawText(rankTextLeft,firstCircleStart + radius,rootTextViewPosition,rankPaint);
        }

        if(!TextUtils.isEmpty(rankTextRight)){
            canvas.drawText(rankTextRight,secondCircleStart+radius,rootTextViewPosition,rankPaint);
        }




    }


    private static void getScreenSize() {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) AppUtils.getInstance().getContext().getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        widthPixels = displayMetrics.widthPixels;
        heightPixels = displayMetrics.heightPixels;
    }



    public void setPersent(float persent){
        this.persent = persent;
        invalidate();
    }


    public void setRankText(String textLeft,String textRight){
        rankTextLeft = textLeft;
        rankTextRight = textRight;
        invalidate();
    }





}
